/**
 * Created with IntelliJ IDEA
 * Description
 * User: 王杰
 * Date: 2025-05-15
 * Time: 16:18
 */

// <E> 表示当前类 是一个泛型类
class MyArray<E> {
    public Object[] array = new Object[10];

    public void setValue(int pos, E val) {
        array[pos] = val;
    }

    public E getValue(int pos) {
        return (E)array[pos];
    }
}

public class Test2 {
    public static void main(String[] args) {
        MyArray<Integer> myArray = new MyArray<Integer>();
        myArray.setValue(0, 10);
        myArray.setValue(1, 20);
//        myArray.setValue(1, "hello");// 自动类型检查
        Integer a =  myArray.getValue(1);
        System.out.println(a);

        MyArray<String> myArray2 = new MyArray<String>();
        myArray2.setValue(0, "hi");
        myArray2.setValue(1, "hello");// 自动类型检查
        String str =  myArray2.getValue(1);
        System.out.println(str);
    }
}
